-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Routes+Reader.swift
168 lines (151 loc) · 4.83 KB
/
Routes+Reader.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import Foundation
enum ReaderRoute {
case root
case discover
case search
case a8c
case p2
case likes
case manageFollowing
case list
case tag
case feed
case blog
case feedsPost
case blogsPost
case wpcomPost
}
extension ReaderRoute: Route {
var path: String {
switch self {
case .root:
return "/read"
case .discover:
return "/discover"
case .search:
return "/read/search"
case .a8c:
return "/read/a8c"
case .p2:
return "/read/p2"
case .likes:
return "/activities/likes"
case .manageFollowing:
return "/following/manage"
case .list:
return "/read/list/:username/:list_name"
case .tag:
return "/tag/:tag_name"
case .feed:
return "/read/feeds/:feed_id"
case .blog:
return "/read/blogs/:blog_id"
case .feedsPost:
return "/read/feeds/:feed_id/posts/:post_id"
case .blogsPost:
return "/read/blogs/:blog_id/posts/:post_id"
case .wpcomPost:
return "/:post_year/:post_month/:post_day/:post_name"
}
}
var section: DeepLinkSection? {
return .reader
}
var action: NavigationAction {
return self
}
}
extension ReaderRoute: NavigationAction {
func perform(_ values: [String: String], source: UIViewController? = nil, router: LinkRouter) {
guard let coordinator = WPTabBarController.sharedInstance().readerCoordinator else {
return
}
// Bounce back to Safari on failure
coordinator.failureBlock = {
self.failAndBounce(values)
}
switch self {
case .root:
coordinator.showReaderTab()
case .discover:
coordinator.showDiscover()
case .search:
coordinator.showSearch()
case .a8c:
coordinator.showA8C()
case .p2:
coordinator.showP2()
case .likes:
coordinator.showMyLikes()
case .manageFollowing:
coordinator.showManageFollowing()
case .list:
if let username = values["username"],
let listName = values["list_name"] {
coordinator.showList(named: listName, forUser: username)
}
case .tag:
if let tagName = values["tag_name"] {
coordinator.showTag(named: tagName)
}
case .feed:
if let feedIDValue = values["feed_id"],
let feedID = Int(feedIDValue) {
coordinator.showStream(with: feedID, isFeed: true)
}
case .blog:
if let blogIDValue = values["blog_id"],
let blogID = Int(blogIDValue) {
coordinator.showStream(with: blogID, isFeed: false)
}
case .feedsPost:
if let (feedID, postID) = feedAndPostID(from: values) {
coordinator.showPost(with: postID, for: feedID, isFeed: true)
}
case .blogsPost:
if let (blogID, postID) = blogAndPostID(from: values) {
coordinator.showPost(with: postID, for: blogID, isFeed: false)
}
case .wpcomPost:
if let urlString = values[MatchedRouteURLComponentKey.url.rawValue],
let url = URL(string: urlString),
isValidWpcomUrl(values) {
coordinator.showPost(with: url)
}
}
}
private func feedAndPostID(from values: [String: String]?) -> (Int, Int)? {
guard let feedIDValue = values?["feed_id"],
let postIDValue = values?["post_id"],
let feedID = Int(feedIDValue),
let postID = Int(postIDValue) else {
return nil
}
return (feedID, postID)
}
private func blogAndPostID(from values: [String: String]?) -> (Int, Int)? {
guard let blogIDValue = values?["blog_id"],
let postIDValue = values?["post_id"],
let blogID = Int(blogIDValue),
let postID = Int(postIDValue) else {
return nil
}
return (blogID, postID)
}
private func isValidWpcomUrl(_ values: [String: String]) -> Bool {
let year = Int(values["post_year"] ?? "") ?? 0
let month = Int(values["post_month"] ?? "") ?? 0
let day = Int(values["post_day"] ?? "") ?? 0
// we assume no posts were made in the 1800's or earlier
func isYear(_ year: Int) -> Bool {
year > 1900
}
func isMonth(_ month: Int) -> Bool {
(1...12).contains(month)
}
func isDay(_ day: Int) -> Bool {
(1...31).contains(day)
}
return isYear(year) && isMonth(month) && isDay(day)
}
}